package controller.lobby
{
	import controller.BaseController;
	import controller.SocketManager;
	import controller.events.SocketEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Label;
	import mx.core.FlexGlobals;
	
	import poker.LevelDefine;
	
	import spark.components.Group;
	import spark.components.Image;

	public class LobbyController extends BaseController
	{
		// static instance 
		public static var instance:LobbyController = null;
		
		public var userId:String;	
		public function LobbyController()
		{
			super();
			if ( instance != null )
				throw new Error( "Only one Singleton instance should be instantiated" ); 
			
		}
		public override function init():void{
			
		}
		public function removeLobbyEventWhenLogOut():void{
			removeEvents();
		}
		static public function get Instance():LobbyController
		{
			if ( instance == null )
				instance = new LobbyController();
			return instance;
		}		
		public function LOBBY_TABLE_LIST():void
		{
			SocketManager.Instance.addEventListener(SocketEvent.LOBBY_TABLE_LIST,LOBBY_TABLE_LIST_HANDLER);
			SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"LOBBY_TABLE_LIST",""));
//			trace("send LOBBY_TABLE_LIST");
		}
		
		public function LOBBY_PLAYER_LIST():void
		{
			SocketManager.Instance.addEventListener(SocketEvent.LOBBY_PLAYER_LIST,LOBBY_PLAYER_LIST_HANDLER);
			SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"LOBBY_PLAYER_LIST",""));
//			trace("send LOBBY_PLAYER_LIST");
		}
		
		public function initEvents():void{
			
			SocketManager.Instance.addEventListener(SocketEvent.LOBBY_NEW_USER_LOGIN,LOBBY_NEW_USER_LOGIN_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.LOBBY_USER_LOG_OUT,LOBBY_USER_LOG_OUT_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.NOTICE_LOBBY_ROOM_USER_JOIN,NOTICE_LOBBY_ROOM_USER_JOIN_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.NOTICE_LOBBY_ROOM_USER_EXIT,NOTICE_LOBBY_ROOM_USER_EXIT_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.NOTICE_LOBBY_ROOM_USER_READY,NOTICE_LOBBY_ROOM_USER_READY_HANDLER);	
			SocketManager.Instance.addEventListener(SocketEvent.NOTICE_LOBBY_ROOM_GAME_START,NOTICE_LOBBY_ROOM_GAME_START_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.NOTICE_LOBBY_ROOM_GAME_OVER,NOTICE_LOBBY_ROOM_GAME_OVER_HANDLER);		
			
		}
		private function removeEvents():void{
			SocketManager.Instance.removeEventListener(SocketEvent.LOBBY_NEW_USER_LOGIN,LOBBY_NEW_USER_LOGIN_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.LOBBY_USER_LOG_OUT,LOBBY_USER_LOG_OUT_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.NOTICE_LOBBY_ROOM_USER_JOIN,NOTICE_LOBBY_ROOM_USER_JOIN_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.NOTICE_LOBBY_ROOM_USER_EXIT,NOTICE_LOBBY_ROOM_USER_EXIT_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.NOTICE_LOBBY_ROOM_USER_READY,NOTICE_LOBBY_ROOM_USER_READY_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.NOTICE_LOBBY_ROOM_GAME_START,NOTICE_LOBBY_ROOM_GAME_START_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.NOTICE_LOBBY_ROOM_GAME_OVER,NOTICE_LOBBY_ROOM_GAME_OVER_HANDLER);
			
		}
		private function NOTICE_LOBBY_ROOM_GAME_OVER_HANDLER(event:SocketEvent):void{
			trace("接收到派发消息：NOTICE_LOBBY_ROOM_GAME_OVER_HANDLER"+event.results[1]);
			controller.ControllerManager.Instance.debug("接收到派发消息：NOTICE_LOBBY_ROOM_GAME_OVER_HANDLER"+event.results[3]);
			if(this.receive(event.results)){
				//				updatePlayerListJoin(this.result);
				//				updateRoomsInfoJoin(this.result);
			}
		}
		private function NOTICE_LOBBY_ROOM_GAME_START_HANDLER(event:SocketEvent):void{
			trace("接收到派发消息：NOTICE_LOBBY_ROOM_USER_START_HANDLER"+event.results[1]);
			controller.ControllerManager.Instance.debug("接收到派发消息：NOTICE_LOBBY_ROOM_USER_START_HANDLER"+event.results[3]);
			if(this.receive(event.results)){
				//				updatePlayerListJoin(this.result);
				//				updateRoomsInfoJoin(this.result);
			}
		}
		private function NOTICE_LOBBY_ROOM_USER_READY_HANDLER(event:SocketEvent):void{
			trace("接收到派发消息：NOTICE_LOBBY_ROOM_USER_READY_HANDLER"+event.results[1]);
			controller.ControllerManager.Instance.debug("接收到派发消息：NOTICE_LOBBY_ROOM_USER_READY_HANDLER"+event.results[3]);
			if(this.receive(event.results)){
//				updatePlayerListJoin(this.result);
//				updateRoomsInfoJoin(this.result);
			}
		}
		private function NOTICE_LOBBY_ROOM_USER_EXIT_HANDLER(event:SocketEvent):void{
			trace("接收到派发消息：NOTICE_LOBBY_ROOM_USER_EXIT_HANDLER"+event.results[1]);
			controller.ControllerManager.Instance.debug("接收到派发消息：NOTICE_LOBBY_ROOM_USER_EXIT_HANDLER"+event.results[3]);
			if(this.receive(event.results)){
//				updatePlayerListJoin(this.result);
				tablePlayersRemove(this.result);
			}
		}
		private function NOTICE_LOBBY_ROOM_USER_JOIN_HANDLER(event:SocketEvent):void{
			trace("接收到派发消息：NOTICE_LOBBY_ROOM_USER_JOIN_HANDLER"+event.results[1]);
			controller.ControllerManager.Instance.debug("接收到派发消息：NOTICE_LOBBY_ROOM_USER_JOIN_HANDLER"+event.results[3]);
			if(this.receive(event.results)){
				tablePlayersJoin(this.result);			
			}
		}
		private function LOBBY_NEW_USER_LOGIN_HANDLER(event:SocketEvent):void{
			trace("接收到派发消息：LOBBY_NEW_USER_LOGIN_HANDLER"+event.results[1]);
		    controller.ControllerManager.Instance.debug("接收到派发消息：LOBBY_NEW_USER_LOGIN_HANDLER"+event.results[3]);
			if(this.receive(event.results)){
				updatePlayerListJoin(this.result);
				updateRoomsInfoJoin(this.result);
			}
		}
		private function updateRoomsInfoJoin(obj:Object):void{
			var state:int=obj.state;
			var pos:int=obj.pos;
			var rid:int=obj.rid;
			if(state!=1&&state!=0&&pos>0&&rid>0){
				tablePlayersJoin(obj);
			}
		}
		private function updateRoomsInfoExit(obj:Object):void{
			var state:int=obj.state;
			var pos:int=obj.pos;
			var rid:int=obj.rid;
			if(state!=1&&state!=0&&pos>0&&rid>0){
				tablePlayersRemove(obj);
			}
		}
		private function updatePlayerListRemove(obj:Object):void{
		var playerlist:ArrayCollection=FlexGlobals.topLevelApplication.playerlist;
			//判断当前玩家在列表不
			for (var i:int=0;i<playerlist.length;i++)
			{
			if(playerlist.getItemAt(i).userId== obj.userId)
				playerlist.removeItemAt(i);
			    break;
			}
			
		}
		private function updatePlayerListJoin(obj:Object):void{
//			var playerlist:ArrayCollection=FlexGlobals.topLevelApplication.playerlist;
			//判断当前玩家在列表不
//			for each(var leftplayer:Object in playerlist)
//			{
//			
//			}		
					var playerdata:Object = new Object();
					playerdata.userId = obj.userId;
					playerdata.nickname = obj.name;
					playerdata.score=obj.score;						
					playerdata.money = obj.money;
					playerdata.level = LevelDefine.getLevelName(playerdata.score);
					FlexGlobals.topLevelApplication.playerlist.addItem(playerdata);	
			// 更新大厅的玩家数据列表
			FlexGlobals.topLevelApplication.roomplayerlist.invalidateList();
		}
		private function LOBBY_USER_LOG_OUT_HANDLER(event:SocketEvent):void{
			trace("接收到派发消息：LOBBY_USER_LOG_OUT_HANDLER"+event.results[1]);
			controller.ControllerManager.Instance.debug("接收到派发消息：LOBBY_USER_LOG_OUT_HANDLER"+event.results[3]);
			if(this.receive(event.results)){
				
				updateRoomsInfoExit(this.result);
			}
		}
		private function LOBBY_TABLE_LIST_HANDLER(event:SocketEvent):void
		{
			SocketManager.Instance.removeEventListener(SocketEvent.LOBBY_TABLE_LIST,LOBBY_TABLE_LIST_HANDLER);
			trace("接收到派发消息:"+event.results[1]+":LOBBY_TABLE_LIST");
			if(this.receive(event.results)){
				RoomTableDrawII(this.result);
				LOBBY_PLAYER_LIST();
			}
			
		}

		private function LOBBY_PLAYER_LIST_HANDLER(event:SocketEvent):void
		{
			SocketManager.Instance.removeEventListener(SocketEvent.LOBBY_PLAYER_LIST,LOBBY_PLAYER_LIST_HANDLER);
			trace("接收到派发消息：LOBBY_PLAYER_LIST"+event.results[1]);
			if(this.receive(event.results)){
				FlexGlobals.topLevelApplication.playerlist.removeAll();
				for each(var leftplayer:Object in this.result.players)
				{
					if(leftplayer.nickname != "")
					{
						var playerdata:Object = new Object();
						playerdata.userId = leftplayer.userId;
						playerdata.nickname = leftplayer.nickname;
						playerdata.score=leftplayer.score;						
						playerdata.money = leftplayer.money;
						playerdata.level = LevelDefine.getLevelName(playerdata.score);
						FlexGlobals.topLevelApplication.playerlist.addItem(playerdata);
					}
				}
				// 更新大厅的玩家数据列表
				FlexGlobals.topLevelApplication.roomplayerlist.invalidateList();
				trace("LOBBY_PLAYER_LIST_HANDLER 更新线先玩家列表成功");
			}
		}	
		
		
		private function RoomTableDrawII(obj:Object):Boolean
		{
			var canvas:Group = FlexGlobals.topLevelApplication.gameRoomCanvas;
			for(var i:int=0;i< 12/*roomTableMax*/;i++)
			{
				if(i >= obj.length || obj[i].rid == -1){
					canvas.getElementAt(i).visible = false;
				}
				else{
					canvas.getElementAt(i).visible = true;
					var gp:Group = canvas.getElementAt(i) as Group;
					// 先进行桌子上的信息的填充
					var tableImg:spark.components.Image = gp.getElementAt(0) as spark.components.Image;
					tableImg.toolTip = obj[i].rname +"\n";				
					var lostp:Array = [0,0,0,0];
					for(var n:int=1;n<=3;n++)
					{
						if(obj[i].players.hasOwnProperty(n.toString()))
						{
							var delta:int = int(obj[i].players[n].pos);
							gp.getElementAt(delta).visible = true;
							tablePlayersDraw(gp ,obj[i].players[n], delta);
							lostp[obj[i].players[n].pos] = 1;
						}
					}
					for(var m:int=1;m<4;m++)
					{
						if(lostp[m]==0){
							gp.getElementAt(3+m).visible = false;
						}
					}
				}
			}
			trace("RoomTableDrawII 绘画房间列表成功");
			return true;
		}
		private function tablePlayersDraw(drawto:Group, obj:Object, idx:int):void
		{
			var player:Group = drawto.getElementAt(3+idx) as Group;
			player.visible=true;
			var avatar:Image = player.getElementAt(0) as Image;
			if(avatar.source != obj.avatar)
			{
				avatar.source = obj.avatar;
			}
			var name:Label = player.getElementAt(1) as Label;
			if(obj.ready)
				name.text = obj.nickname +"(o)";
			else
				name.text = obj.nickname +"(x)";
			
			var describe:String = "";
			describe += obj.score;
			describe += "分 ";
			describe += LevelDefine.getLevelName(int(obj.score));
			describe += "\n";
			describe += obj.money + " 金币";
			name.text=obj.nickname;
			name.toolTip = obj.nickname + "\n" + describe;
			avatar.toolTip = obj.nickname + "\n" + describe;
		}
		public function tablePlayersRemove(obj:Object):void
		{
			var canvas:Group = FlexGlobals.topLevelApplication.gameRoomCanvas;
			var drawto:Group = canvas.getElementAt(obj.rid-1) as Group;
			var player:Group = drawto.getElementAt(3+obj.pos) as Group;
			player.visible=false;
			return;
			var avatar:Image = player.getElementAt(0) as Image;
			avatar.source = obj.avatar;
			var name:Label = player.getElementAt(1) as Label;
			if(obj.ready)
				name.text = obj.nickname +"(o)";
			else
				name.text = obj.nickname +"(x)";
			
			var describe:String = "";
			describe += obj.score;
			describe += "分 ";
			describe += LevelDefine.getLevelName(int(obj.score));
			describe += "\n";
			describe += obj.money + " 金币";
			name.text=obj.nickname;
			name.toolTip = obj.nickname + "\n" + describe;
			avatar.toolTip = obj.nickname + "\n" + describe;
		}
		public function tablePlayersJoin(obj:Object):void
		{
			var canvas:Group = FlexGlobals.topLevelApplication.gameRoomCanvas;
			var drawto:Group = canvas.getElementAt(obj.rid-1) as Group;
			var player:Group = drawto.getElementAt(3+obj.pos) as Group;
			player.visible=true;
			var avatar:Image = player.getElementAt(0) as Image;
			if(avatar.source != obj.avatar)
			{
				avatar.source = obj.avatar;
			}
			var name:Label = player.getElementAt(1) as Label;
			if(obj.ready)
				name.text = obj.nickname +"(o)";
			else
				name.text = obj.nickname +"(x)";
			
			var describe:String = "";
			describe += obj.score;
			describe += "分 ";
			describe += LevelDefine.getLevelName(int(obj.score));
			describe += "\n";
			describe += obj.money + " 金币";
			name.text=obj.nickname;
			name.toolTip = obj.nickname + "\n" + describe;
			avatar.toolTip = obj.nickname + "\n" + describe;
		}
	}	
}